﻿using GoodDotNet.Core.Model;
using GoodDotNet.Core.Model.Enum;
using GoodDotNet.WebAdmin.HttpContextUser;
using Microsoft.Extensions.Options;
using GoodDotNet.Core.Data;

namespace GoodDotNet.WebAdmin.Controllers
{
    public class BaseController
    {
        protected IUser _user;
        protected readonly HttpContext? _context;
        protected readonly List<ConnectionString> _conns;
        protected ILogger _logger;
        protected Appsettings _appsettings;


        public BaseController(IUser user, IHttpContextAccessor accessor, IOptions<List<ConnectionString>> conns,ILogger<BaseController> logger, IOptions<Appsettings> appsettings)
        {
            this._user = user;
            this._context = accessor.HttpContext;
            this._conns = conns.Value;
            this._logger = logger;
            this._appsettings = appsettings.Value;
        }


        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <param name="name">数据库名称</param>
        /// <returns></returns>
        /// 
        protected ConnectionString _conStr
        {
        get { return _conStrGet("master"); }
        }
       
        protected ConnectionString _conStrGet(string name)
        {
            var c = _conns.SingleOrDefault(c => c.Name == name);
            if(c == null)
            {
                _logger.LogError(@"没有定义数据库连接：{name}",name);
                throw new Exception();
            }
            return c;
        }
        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected Result Sucess(object result)
        {
            return new Result
            {
                code = EnumErrorCode.ErrorCode.Sucess,
                result = result
            };
        }
        /// <summary>
        /// 捕获的错误
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected Result CatchError(string message)
        {
            return new Result
            {
                code = EnumErrorCode.ErrorCode.CatchError,
                result = message
            };
        }
        /// <summary>
        /// 签名错误
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected Result SignError(string message)
        {
            return new Result
            {
                code = EnumErrorCode.ErrorCode.SignError,
                result = message
            };
        }
        /// <summary>
        /// 没有登录
        /// </summary>
        /// <returns></returns>
        protected Result NoLogin()
        {
            return new Result
            {
                code = EnumErrorCode.ErrorCode.NoLogin,
                result = "您还没有登录"
            };
        }

        /// <summary>
        /// 没有权限
        /// </summary>
        /// <returns></returns>
        protected Result PermissionDenied()
        {
            return new Result
            {
                code = EnumErrorCode.ErrorCode.PermissionDenied,
                result = "你没有权限"
            };
        }
        /// <summary>
        ///  一般错误
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>

        protected Result Error(string message)
        {
            return new Result
            {
                code = EnumErrorCode.ErrorCode.Error,
                result = message
            };
        }

    }
}
